/*jslint white: true, browser: true, undef: true, nomen: true, eqeqeq: true, plusplus: false, bitwise: true, regexp: true, strict: true, newcap: true, immed: true, maxerr: 14 */
/*global window: false, REDIPS: true, $: true */

/* enable strict mode */
"use strict";

// define functions
var redips_init,
	load_table,
	rd;

	
// redips initialization
redips_init = function () {
    // reference to the REDIPS.drag lib
    var msg = document.getElementById('message');
    rd = REDIPS.drag;
    // initialization
    rd.init();
    rd.trash_ask = false;


//    rd.mark.exception.placec0 = 'place_cell';
//    rd.mark.exception.people0 = 'people_cell';
//    rd.mark.exception.event0 = 'event_cell';
//    rd.mark.exception.activity0 = 'activity_cell';

    //	for (var i = 2; i < 9; i++) {
    //	    var tst = 'greenc' + i;
    //	    rd.mark.exception.tst;
    //	}




    rd.myhandler_clicked = function () {
        msg.innerHTML = 'Clicked';
    };
    rd.myhandler_dblclicked = function () {
        msg.innerHTML = 'Dblclicked';
    };
    rd.myhandler_moved = function () {
        msg.innerHTML = 'Moved';
    };
    rd.myhandler_notmoved = function () {
        msg.innerHTML = 'Not moved';
    };
    rd.myhandler_dropped = function () {
        msg.innerHTML = 'Dropped a ' + $("#" + rd.obj.id).attr("obj");
    };
    rd.myhandler_switched = function () {
        msg.innerHTML = 'Switched';
    };
    rd.myhandler_clonedend1 = function () {
        msg.innerHTML = 'Cloned end1';
    };
    rd.myhandler_clonedend2 = function () {
        msg.innerHTML = 'Cloned end2';
    };
    rd.myhandler_notcloned = function () {
        msg.innerHTML = 'Not cloned';
    };
    rd.myhandler_deleted = function (cloned) {
        // if cloned element is directly moved to the trash
        if (cloned) {
            // set id of original element (read from redips property)
            // var id_original = rd.obj.redips.id_original;
            msg.innerHTML = 'Deleted (c)';
        }
        else {
            msg.innerHTML = 'Deleted';
        }
    };
    rd.myhandler_undeleted = function () {
        msg.innerHTML = 'Undeleted';
    };
    rd.myhandler_cloned = function () {
        // display message
        msg.innerHTML = 'Cloned';
        // append 'd' to the element text (Clone -> Cloned)
        // rd.obj.innerHTML += 'd';
    };
    rd.myhandler_changed = function () {
        // get target and source position (method returns positions as array)
        var pos = rd.get_position();
        // display current row and current cell
        msg.innerHTML = 'Changed: ' + pos[1] + ' ' + pos[2];
    };
};


// new table using AJAX/jQuery to the drag container 
load_table = function (button) {
	// parameter (example for ajax request)
	var id = 1;
	// disable button (it can be clicked only once)
	button.style.backgroundColor = '#c0c0c0';
	button.disabled = true;
	// AJAX request
	$.ajax({
		type: 'get',
		url: 'ajax.php',
		data: 'id=' + id,
		cache: false,
		success: function (result) {
			// load new table
			$('#load_content').html(result);
			// run REDIPS.drag initialization
			rd.init();
		}
	});
};


// add onload event listener
if (window.addEventListener) {
	window.addEventListener('load', redips_init, false);
}
else if (window.attachEvent) {
	window.attachEvent('onload', redips_init);
}